home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7764 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  94 lines

  1. Path: fc.hp.com!not-for-mail
  2. From: neutron@fc.hp.com (Jack Applin)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: gettimeofday() makes me mad !
  5. Date: 28 Feb 1996 20:23:29 GMT
  6. Organization: Hewlett-Packard, Ft. Collins, CO
  7. Distribution: inet
  8. Message-ID: <4h2dk1$e3i@fcnews.fc.hp.com>
  9. References: <4gnkth$4on@piston.ecp.fr>
  10. NNTP-Posting-Host: jackbert.fc.hp.com
  11. X-Newsreader: TIN [UNIX 1.3 310394BETA PL0]
  12.  
  13. BIG ONE (dureta8@cti.ecp.fr) wrote:
  14.  
  15. > PROBLEM : I'm trying to make a little program which must repeat an
  16. > action every .2 second for example ( less than 1 sec anyway). time()
  17. > wouldn't fit, so I searched and found the function gettimeofday() in
  18. > <sys/time.h>. It gives the time elapsed since 1970 in seconds and
  19. > microseconds. The problem is that when gettimeofday() is called several
  20. > times, it finally hangs up ! My program looks like this : 
  21. >
  22. >   struct timeval t ;
  23. >   long useconds ;
  24. >
  25. >   gettimeofday(&t) ;
  26. >   printf("%ld\n",(usecondes = t.tv_usec)) ;
  27. >
  28. >   for (;;) {
  29. >     /* ... */ /*this part works fine */
  30. >     gettimeofday(&t) ;
  31. >     if (t.tv_usec > usecondes + 200000) {
  32. >       printf("%ld\n",(usecondes = t.tv_usec)) ;
  33. >       do_smthg() ;
  34. >     } 
  35. >   }
  36.  
  37.  
  38. Your problem is that you're only looking at the microseconds.  You have to compare
  39. both the seconds (tv_sec) and the microseconds (tv_usec) to the previous time.
  40. Here's a cheap way of doing it using floating-point arithmetic:
  41.  
  42. #include <stdio.h>
  43. #include <time.h>
  44.  
  45. main()
  46. {
  47.    struct timeval t ;
  48.    double previous, now;
  49.  
  50.    gettimeofday(&t) ;
  51.  
  52.    previous = t.tv_sec + t.tv_usec / 1000000.0;    /* get a floating-point time */
  53.  
  54.    for (;;) {
  55.      /* ... */ /*this part works fine */
  56.      gettimeofday(&t) ;
  57.      now = t.tv_sec + t.tv_usec / 1000000.0;
  58.      if (now > previous + 0.2) {
  59.        previous = now;
  60.        puts("hi");
  61.      }
  62.    }
  63. }
  64.  
  65. This may be inefficient, because it uses floating-point arithmetic,
  66. but it works.  Another way would be to compare both the seconds and the
  67. microseconds of the times, like this:
  68.  
  69. /* UNTESTED CODE */
  70. main()
  71. {
  72.    struct timeval previous, now;
  73.    long diff;
  74.  
  75.    gettimeofday(&previous) ;
  76.  
  77.    for (;;) {
  78.      /* ... */ /*this part works fine */
  79.      gettimeofday(&now) ;
  80.      diff = (now.tv_sec - previous.tv_sec)*1000000
  81.        +(now.tv_usec - previous.tv_usec);
  82.      if (diff > 2000000) {
  83.        previous = now;
  84.        puts("hi");
  85.      }
  86.    }
  87. }
  88.  
  89.  
  90.  
  91.                 -Jack Applin
  92.                  neutron@fc.hp.com
  93.                  http://www.cs.colostate.edu/~heckendo/Jack/
  94.